﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AVCare.LMS.DataAccess;
using AVCare.LMS.Models;
using System.IO;
using AVCare.LMS.Util;

namespace AVCare.LMS.LocalData
{
    public class FileHelper : IFileHelper
    {
        LMSEntities entities = new LMSEntities();
        #region IFileHelper 成员

        public List<Folder> GetFolders(Guid folderID)
        {
            var query = from t in entities.Folders
                        where t.ParentID == folderID && !t.IsRoot
                        select t;


            return query.ToList();

        }

        public List<AVCare.LMS.Models.File> GetFiles(Guid folderID)
        {
            var query = from f in entities.Files
                        where f.FolderID == folderID
                        select f;
            return query.ToList();
        }


        public Guid GetRoot(string userID)
        {
            var query = entities.Folders.FirstOrDefault(f => f.UserID == userID && f.IsRoot);

            if (query != null)
            {
                return query.ID;
            }
            return new Guid();
        }

        public Guid GetParent(Guid folderID)
        {
            var query = entities.Folders.FirstOrDefault(f => f.ID == folderID);

            if (query != null)
            {
                return query.ParentID;
            }
            return new Guid();
        }

        public AVCare.LMS.Models.File GetFile(Guid fileID)
        {
            var query = entities.Files.FirstOrDefault(f => f.ID == fileID);

            if (query != null)
            {
                return query;
            }
            return new AVCare.LMS.Models.File();
        }

        public List<Folder> GetShareFolder()
        {
            var query = from f in entities.Folders
                        where
                            (from sf in entities.SharedFloders where sf.Role != (int)ShareRole.NoShare select sf.Floder).Contains(f.ID)
                        select f;

            return query.ToList();
        }

        public void SaveFile(AVCare.LMS.Models.File file)
        {
            entities.Files.Add(file);
            entities.SaveChanges();
        }


        public Folder GetFolder(Guid parentID, string path, string userID)
        {
            string[] folders = path.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
            if (folders.Length == 0)
            {
                return GetFolder(parentID);
            }
            Folder f = null;
            string fpath = string.Empty;
            foreach (var folder in folders)
            {
                var query = entities.Folders.FirstOrDefault(a => a.ParentID == parentID && a.Name == folder);
                if (query != null)
                {
                    parentID = query.ID;
                    f = query;
                    fpath = Path.Combine(fpath, f.Path);
                    continue;
                }

                f = new Folder();
                f.CreateTime = DateTime.Now;
                f.ID = Guid.NewGuid();
                f.IsRoot = false;
                f.Name = folder;
                f.ParentID = parentID;
                fpath = Path.Combine(fpath, folder);
                f.Path = fpath;
                f.UserID = userID;
                parentID = f.ID;

                entities.Folders.Add(f);
            }
            entities.SaveChanges();
            return f;

        }

        public Folder GetFolder(Guid folderID)
        {
            var query = entities.Folders.FirstOrDefault(f => f.ID == folderID);
            return query;
        }

        public void ReNameFolder(Guid id, string name)
        {
            var query = entities.Folders.FirstOrDefault(f => f.ID == id);
            query.Name = name;
            entities.SaveChanges();
        }

        public Folder CreateFolder(Guid parentID, string name, string userID)
        {
            Folder folder = new Folder();
            folder.CreateTime = DateTime.Now;
            folder.ID = Guid.NewGuid();
            folder.IsRoot = false;
            folder.Name = name;
            folder.ParentID = parentID;

            var parentPath = entities.Folders.FirstOrDefault(f => f.ID == parentID).Path;

            folder.Path = Path.Combine(parentPath, name);
            folder.UserID = userID;

            entities.Folders.Add(folder);
            entities.SaveChanges();
            return folder;
        }

        public void ReNameFile(Guid id, string name)
        {
            var query = entities.Files.FirstOrDefault(f => f.ID == id);
            query.FileName = name;
            query.LastEditTime = DateTime.Now;
            entities.SaveChanges();
        }

        public void DeleteFolder(Guid id)
        {
            var files = from f in entities.Files
                        where f.FolderID == id
                        select f;
            foreach (var file in files.ToList())
            {
                DeleteFile(file.ID);
            }

            var children = from f in entities.Folders
                           where f.ParentID == id
                           select f;

            foreach (var folder in children.ToList())
            {
                DeleteFolder(folder.ID);
            }

            var curFolder = entities.Folders.FirstOrDefault(f => f.ID == id);
            entities.Folders.Remove(curFolder);

            entities.SaveChanges();
        }

        public void DeleteFile(Guid id)
        {
            var file = entities.Files.FirstOrDefault(f => f.ID == id);
            entities.Files.Remove(file);
            entities.SaveChanges();
        }

        public SharedFolder GetShareFolder(Guid id)
        {
            return entities.SharedFloders.FirstOrDefault(f => f.Floder == id);
        }

        #endregion

        #region IFileHelper 成员


        public void ShareFolder(Guid id, string password, Util.ShareRole role)
        {
            SharedFolder sf;
            sf = entities.SharedFloders.FirstOrDefault(f => f.Floder == id);
            if (sf == null)
            {
                sf = new SharedFolder();
                sf.ID = Guid.NewGuid();
                entities.SharedFloders.Add(sf);
            }

            sf.Floder = id;
            sf.Password = password;
            sf.Role = (int)role;

            entities.SaveChanges();
        }

        #endregion
    }
}